package controller

import (
	"github.com/gofiber/fiber"
	jsoniter "github.com/json-iterator/go"
	"gowork/shuttleBus/model"
	"strconv"
	"time"
)

type ShuttleBusController struct {
	Route *model.Route
}

func (sbc *ShuttleBusController)CreateRoute(c *fiber.Ctx){

	sbc.Route = new(model.Route)

	var route model.Route
	c.BodyParser(&route)

	res , err := sbc.Route.CreateRoute(&route)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": nil,
		"message": res["message"],
	})
	return
}

func (sbc *ShuttleBusController)QueryRoute(c *fiber.Ctx){

	sbc.Route = new(model.Route)

	routeIdStr := c.FormValue("routeId")
	routeId, err := strconv.ParseInt(routeIdStr,10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := sbc.Route.QueryRoute(routeId)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": "200",
		"data": res,
		"message": "成功",
	})
	return
}

func (sbc *ShuttleBusController)AppointmentShuttleBus(c *fiber.Ctx){

	sbc.Route = new(model.Route)

	staffIdStr := c.FormValue("staffId")
	staffId, err := strconv.ParseInt(staffIdStr,10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	var routeList []*model.Route
	routeListStr := c.FormValue("routeList")
	uErr := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(routeListStr), &routeList)
	if uErr != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": uErr.Error(),
		})
		return
	}

	var dateList []string
	dateListStr := c.FormValue("dateList")
	dErr := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(dateListStr), &dateList)
	if dErr != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": dErr.Error(),
		})
		return
	}

	res , err := sbc.Route.AppointmentShuttleBus(staffId, routeList, dateList)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": nil,
		"message": res["message"],
	})
	return
}

func (sbc *ShuttleBusController)ShuttleBus(c *fiber.Ctx){

	sbc.Route = new(model.Route)

	dayTimeStr := c.FormValue("dayTime")
	loadLocation, _ := time.LoadLocation("Asia/Shanghai")
	dayTime,err := time.ParseInLocation("2006-01-02", dayTimeStr, loadLocation)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := sbc.Route.ShuttleBus(dayTime)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": "200",
		"data": res,
		"message": "成功",
	})
	return
}

func (sbc *ShuttleBusController)ByCar(c *fiber.Ctx){

	sbc.Route = new(model.Route)

	staffIdStr := c.FormValue("staffId")
	staffId, err := strconv.ParseInt(staffIdStr,10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	routeIdStr := c.FormValue("routeId")
	routeId, err := strconv.ParseInt(routeIdStr,10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := sbc.Route.ByCar(staffId, routeId)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": nil,
		"message": res["message"],
	})
	return
}
